var windowName = "myAddressAdd";
var Page,bMap,searchIndex,count = 0;
apiready = function(){
    onWindowShow(function () {
        if (count == 0){count++;return;}
        var data = getLocData("mapPickerData")
        logJson(data,"mapData: ")
        if (isTrue(data)) {
            Page.mapPickerData = data
            Page.mapPickerData.show = true
            Page.mapPickerDataShow = true
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"title":"众成明月华庭","uid":"40fd5e69ab91616aee74f80f","point":{"lng":118.657893,"lat":37.411411},"city":"东营市","Si":"房地产","type":0,"address":"山东省东营市东营区G220(南二路)","postcode":null,"phoneNumber":null,"lu":["房地产"]}
            // {"lon":118.675322,"lat":37.434984,"name":"众成商城","uid":"afc018bde930ec6438e4a64c","address":"168路","city":"东营市","phone":""}

            //使用这种格式
            // {"lon":118.638399,"lat":37.440736,"name":"众成禧园","uid":"1a7329e3be8fb3c44f6d4637","address":"东一路与辽河路交叉口东南150米","city":"东营市","phone":""}
            Page.lat = data.lat
            Page.lon = data.lon
            // Page.city = data.city
        }
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        id:0,
        lon:0,
        lat: 0,
        code:0,
        province:null,
        city:null,
        area:null,
        address:"",
        tags:[]

        //标签相关
        ,tagPool:["有电梯","无电梯","市区","郊区"]

        //地图相关
        ,mapPickerData:{}
        ,mapPickerDataShow:false
        //省市县去选择
        ,orgData:[]
        ,pro:{}
        ,proIndex:0
        ,cit:{}
        ,citIndex:0
        ,are:{}
        ,areIndex:0
        ,pcaStep:1
        ,pcaShow:false
    },{
        //页面方法
        init:function () {
            var data = getData4Page();
            var id = data.id;
            this.id = id
            log(id,"id_:")
            if (isTrue(id)) {
                getData();
            }
            this.orgData = citys;
            // var _this = this;
            // logJson(citys,"citys")
        },
        submit: function () {
            if (!isTrue(this.lon)){showToast("请拾取位置坐标");return;}
            if (!isTrue(this.code)){showToast("请选择县区");return;}
            if (!isTrue(this.address)){showToast("请填写详细地址");return;}
            var data= {
                lon:this.lon,
                lat:this.lat,
                code:this.code,
                province:this.province,
                city:this.city,
                area:this.area,
                address:this.address,
                tags:isTrue(this.tags)?this.tags:[],
            };
            if (isTrue(this.id)) {
                data.id = this.id
            }
            apiPost("/api/userAddresss", data, function (res, code) {
                if (code == 200) {
                    showToast(res.message);
                    gobackDelay()
                }else {
                    showToast(res.message);
                }
            });

        }

        ,pick:function () {
            goto('mapPicker')
        }
        ,addTag:function (item) {
            this.tags.push(item);
            this.tagPool = arrayDelValue(this.tagPool,item);
        }
        ,delTag:function (item) {
            this.tagPool.push(item);
            this.tags = arrayDelValue(this.tags,item);
        }
        , setAddress: function (text) {
            if (Page.address == null) {
                Page.address = ""
            }
            Page.address += text;
        }
        , hideFastInput: function () {
            logJson(Page.mapPickerData.show)
            Page.mapPickerDataShow = !Page.mapPickerDataShow
        }

        //地区选择器相关
        , pcaShowFunc:function () {
            this.pcaShow = !this.pcaShow
        }
        , setP: function (proIndex,pro) {
            this.pro = pro
            this.proIndex = proIndex
            this.pcaStep = 2
            logJson(this.pcaStep)
            logJson(pro)
        }
        , setC: function (citIndex,cit) {
            this.cit = cit
            this.citIndex = citIndex
            this.pcaStep = 3
        }
        , setA: function (areIndex,are) {
            this.are = are
            this.areIndex = areIndex
            this.pcaStep = 1
            //todo 设置选择完成后的逻辑，手动关闭不算
            this.province = this.pro.name
            this.city = this.cit.name
            this.area = this.are.name
            this.code = this.are.id
            this.pcaShow = false;
        }
    })
    Page.init()
}
function getData() {
    apiGet("/api/userAddresss/" + Page.id, {}, function (res, code) {
        if (code == 200) {
            Page.id=res.data.id;
            Page.lon=res.data.lon;
            Page.lat=res.data.lat;
            Page.code=res.data.code;
            Page.province=res.data.province;
            Page.city=res.data.city;
            Page.area=res.data.area;
            Page.address=res.data.address;
            // Page.tags=res.data.tags;
            if (isTrue(res.data.tags)){
                res.data.tags.map(function (d) {
                    Page.addTag(d)
                })
            }
            // Page.id=res.data.id;
            //     :0,
            //     : 0,
            //     :0,
            //     :null,
            //     :null,
            //     :null,
            //     :"",
            //     :[]


            // setAddress(res.data)
            // showToast(res.message);
        }else {

        }
        logJson(res,"/app/my/address_: ");
    });
}